package com.hrms.web.sys_role.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hrms.utils.ResultUtils;
import com.hrms.utils.ResultVo;
import com.hrms.web.sys_role.entity.RoleParm;
import com.hrms.web.sys_role.entity.SelectItem;
import com.hrms.web.sys_role.entity.SysRole;
import com.hrms.web.sys_role.service.SysRoleService;
import com.hrms.web.sys_role_menu.entity.SaveMenuParm;
import com.hrms.web.sys_role_menu.service.SysRoleMenuService;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.chrono.ChronoLocalDateTime;
import java.util.*;

/**
 * @Title: SysRoleController
 * @Author zhanji
 * @Package com.hrms.web.sys_role.controller
 * @description:
 */
@RequestMapping("/api/role")
@RestController
public class SysRoleController {

    @Resource
    private SysRoleService sysRoleService;

    @Resource
    private SysRoleMenuService sysRoleMenuService;

    //新增
    @PostMapping
    public ResultVo add(@RequestBody SysRole sysRole){
        sysRole.setCreateTime(LocalDateTime.now());
        if (sysRoleService.save(sysRole)){
            return ResultUtils.success("新增成功");
        }
        return ResultUtils.error("新增失败");
    }

    //编辑
    @PutMapping
    public ResultVo edit(@RequestBody SysRole sysRole){
        sysRole.setUpdateTime(LocalDateTime.now());
        if (sysRoleService.updateById(sysRole)){
            return ResultUtils.success("编辑成功");
        }
        return ResultUtils.error("编辑失败");
    }

    //删除
    @DeleteMapping("/{roleId}")
    public ResultVo delete(@PathVariable("roleId") Long roleId){
        if (sysRoleService.removeById(roleId)){
            return ResultUtils.success("删除成功");
        }
        return ResultUtils.error("删除失败");
    }

    //分页
    @GetMapping("/getList")
    public ResultVo getList(RoleParm parm){
        //构造分页对象
        IPage<SysRole> page =new Page<>(parm.getCurrentPage(), parm.getPageSize());
        //构造查询条件
        QueryWrapper<SysRole> query = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(parm.getRoleName())){
            query.lambda().like(SysRole::getRoleName, parm.getRoleName());
        }
        query.lambda().orderByDesc(SysRole::getCreateTime);
        IPage<SysRole> list = sysRoleService.page(page, query);
        return ResultUtils.success("查询成功",list);
    }

    //角色下拉数据列表
    @GetMapping("/selectList")
    public ResultVo selectList(){
        List<SysRole> list = sysRoleService.list();
        //返回的值
        List<SelectItem> selectItems = new ArrayList<>();
        Optional.ofNullable(list).orElse(new ArrayList<>())
            .forEach(item->{
                SelectItem selectItem = new SelectItem();
                selectItem.setChecked(false);
                selectItem.setLabel(item.getRoleName());
                selectItem.setValue(item.getRoleId());
                selectItems.add(selectItem);
            });
        return ResultUtils.success("查询成功",selectItems);
    }

    //保存角色菜单
    @PostMapping("/saveRoleMenu")
    public ResultVo saveRoleMenu(@RequestBody SaveMenuParm parm){
        sysRoleMenuService.saveRoleMenu(parm);
        return ResultUtils.success("分配成功");
    }
}
